<?php

namespace App\Admin\Resources;

use App\Admin\Utils\Admin;

/**
 * @mixin \App\Admin\Models\AdminUser
 */
class AdminUserResource extends JsonResource
{
    const FOR_INFO = 'info';
    const FOR_EDIT_INFO = 'edit_info';
    const FOR_EDIT = 'edit';
    const FOR_INDEX = 'index';
    const FOR_SHOW = 'show';

    /**
     * Transform the resource into an array.
     *
     * @param \Illuminate\Http\Request $request
     *
     * @return array
     */
    public function toArray($request)
    {
        $data = [
            'id' => $this->id,
            'username' => $this->username,
            'name' => $this->name,
            'avatar' => $this->avatar,
            'roles' => $this->mergeRoles(),
            'permissions' => $this->mergePermissions(),
            'created_at' => (string) $this->created_at,
            'updated_at' => (string) $this->updated_at,
        ];
        return $data;
    }

    protected function mergeRoles()
    {
        return [
            $this->mergeFor(static::FOR_INFO, function () {
                return $this->roles()->withTrashed(isRestore())->pluck('slug');
            }),
            $this->mergeFor(static::FOR_EDIT, function () {
                return $this->roles()->withTrashed(isRestore())->pluck('id');
            }),
            $this->mergeFor(static::FOR_EDIT_INFO, function () {
                return $this->roles()->withTrashed(isRestore())->pluck('name');
            }),
            $this->mergeFor(static::FOR_INDEX, function () {
                return $this->roles()->withTrashed(isRestore())->pluck('name');
            }),
            $this->mergeFor(static::FOR_SHOW, function () {
                return $this->roles()->withTrashed(isRestore())->pluck('name');
            }),
        ];
    }

    protected function mergePermissions()
    {
        return [
            $this->mergeFor(static::FOR_INFO, function () {
                return $this->allPermissions()->pluck('slug');
            }),
            $this->mergeFor(static::FOR_EDIT, function () {
                return $this->permissions()->select(['id', 'name'])->get();
            }),
            $this->mergeFor(static::FOR_EDIT_INFO, function () {
                return $this->allPermissions()->pluck('name');
            }),
            $this->mergeFor(static::FOR_INDEX, function () {
                return $this->permissions()->pluck('name');
            }),
            $this->mergeFor(static::FOR_SHOW, function () {
                return $this->permissions()->pluck('name');
            }),
        ];
    }

    public function adminUserPermissions()
    {
        $permissions = [];
        foreach ($this->allPermissions()->pluck('slug') as $key => $value) {
            if (Admin::isAdministrator()) {
                $permissions[$key]['method'] = ['all'];
                $permissions[$key]['path'] = $value;
            } else {
                $valueArr = explode('-', $value);
                $router = implode('-', array_slice($valueArr, 2));
                if ($router) {
                    $method = strtolower($valueArr[1]);
                    $permissions[$key]['path'] = $router;
                    if ($method=='all') {
                        $permissions[$key]['method'] = ['get','post','put','delete'];
                    } else {
                        $permissions[$key]['method'][] = $method;
                    }
                }
            }
        }
        return array_values($permissions);
    }
}
